package shop.service;

import org.flown.shop.service.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.dao.GoodsCommentDAO;
import shop.entity.GoodsComment;

import java.io.File;
import java.io.IOException;
import java.util.List;


@Service
public class GoodsCommentService extends BaseService<GoodsComment>
{
    private final GoodsCommentDAO dao_goods_comment;

    public GoodsCommentService(GoodsCommentDAO dao_goods_comment) {
        super(dao_goods_comment);
        this.dao_goods_comment = (GoodsCommentDAO) this.dao;
    }

    public GoodsComment getGoodsCommentByID(int id) {return this.selectByID(id);}

    @Transactional
    public int insertGoodsComment(GoodsComment comment) {
        int result = this.insert(comment);
        if (result == 0)
            return 0;
        try {comment.savePictures();}
        catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);}
        return result;
    }

    @Transactional
    public boolean updateGoodsCommentByID(GoodsComment original_comment)
    {
        if (!this.updateByID(original_comment))
            return false;
        try {original_comment.savePictures();}
        catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);}
        return true;
    }

    public int deleteGoodsCommentByID(int id)
    {
        GoodsComment comment = this.getGoodsCommentByID(id);
        int result = this.deleteByID(id);
        if (result == 0)
            return 0;
        File file = new File(comment.getDirectoryRealPath());
        file.delete();
        return result;
    }

    public List<GoodsComment> getGoodsCommentByUserID(int user_id)
    {
        return this.dao_goods_comment.selectByUserID(user_id);
    }
    public List<GoodsComment> getGoodsCommentByOrdersID(int orders_id)
    {
        return this.dao_goods_comment.selectByOrdersID(orders_id);
    }
}


